<?php
	//系统分隔符
	define('DS',DIRECTORY_SEPARATOR);
	//系统根目录
	define('ROOT',dirname(__FILE__));
	require_once(ROOT. DS.'config'.DS.'eloquent.php');
	require_once(ROOT. DS.'core'.DS.'Util.class.php');
	use core\App;
	use core\Util;
	###########################编写更新###############################
	//基础用户表
	APP::schema()->dropIfExists('sys_users');
	App::schema()->create('sys_users',function($table){
		$table->bigIncrements('id');
		$table->string('login_name',30)->unique();
		$table->string('login_pwd',50);
		$table->tinyInteger('is_admin')->default(0);
		$table->string('email',50)->nullable();
		$table->string('qq',20)->nullable();
		$table->timestamp('create_time');
		$table->timestamp('edit_time')->nullable();
	});
	APP::DB()->table('sys_users')->insert([
		[
			'login_name' => 'admin',
			'login_pwd' => md5('admin'),
			'is_admin' => 1,
			'create_time' =>Util::time(),
			'edit_time' =>Util::time()
		]
    ]);
	
	//角色表
	APP::schema()->dropIfExists('sys_role');
	App::schema()->create('sys_role',function($table){
		$table->bigIncrements('id');
		$table->string('name',30)->unique();
		$table->timestamp('create_time');
	});
	APP::DB()->table('sys_role')->insert([
		['name' => '普通会员',
		'create_time' =>Util::time()],
		['name' => '高级会员',
		'create_time' =>Util::time()],
		['name' => '金牌会员',
		'create_time' =>Util::time()],
    ]);
	//角色用户关系
	APP::schema()->dropIfExists('sys_role_user');
	App::schema()->create('sys_role_user',function($table){
		$table->bigIncrements('id');
		$table->bigInteger('user_id');
		$table->bigInteger('role_id');
	});
	
	//字典表
	APP::schema()->dropIfExists('sys_dic');
	App::schema()->create('sys_dic',function($table){
		$table->bigIncrements('id');
		$table->string('name',50);
		$table->string('key',50)->nullable()->unique();
		$table->string('value',100)->nullable();
		$table->bigInteger('kind_id')->nullable();
		$table->bigInteger('orders')->nullable();
		$table->tinyInteger('status')->default(1); //0:1是否有效
		$table->timestamp('create_time');
	});
	APP::DB()->table('sys_dic')->insert([
		'name' => '状态',
		'key' => 'status',
		'create_time' => Util::time()
    ]);
	APP::DB()->table('sys_dic')->insert([
		'name' => '菜单分类',
		'key' => 'menu_type',
		'create_time' => Util::time()
    ]);
	APP::DB()->table('sys_dic')->insert([
		[
			'name' => '是',
			'kind_id' => 1,
			'value'=>1,
			'orders' => 1,
			'create_time' => Util::time()],
		[
			'name' => '否',
			'value'=>0,
			'kind_id' => 1,
			'orders' => 2,
			'create_time' => Util::time()
		]	
    ]);
	APP::DB()->table('sys_dic')->insert([
		[
			'name' => '模块',
			'value'=>1,
			'kind_id' => 2,
			'orders' => 1,
			'create_time' => Util::time()],
		[
			'name' => '分组',
			'value'=>2,
			'kind_id' => 2,
			'orders' => 2,
			'create_time' => Util::time()
		],
		[
			'name' => '菜单',
			'value'=>3,
			'kind_id' => 2,
			'orders' => 3,
			'create_time' => Util::time()
		],
		[
			'name' => '方法',
			'value'=>4,
			'kind_id' => 2,
			'orders' => 4,
			'create_time' => Util::time()
		]			
    ]);
	
	
	//资源表
	APP::schema()->dropIfExists('sys_menu');
	App::schema()->create('sys_menu',function($table){
		$table->bigIncrements('id');
		$table->string('name',30);
		$table->string('icon',20)->nullable();
		$table->string('key',30)->unique();
		$table->string('parent_key',30)->nullable();
		$table->string('url',200)->nullable();
		$table->bigInteger('parent_id')->nullable();
		$table->bigInteger('orders')->default(1)->nullable(); //排序
		$table->tinyInteger('type')->default(3); //1：模块 2：分组 3：菜单  4:方法
		$table->tinyInteger('is_leaf')->nullable(); //是否显示
		$table->tinyInteger('is_show')->default(1); //是否显示
		
	});
	//角色与资源关系
	APP::schema()->dropIfExists('sys_role_menu');
	App::schema()->create('sys_role_menu',function($table){
		$table->bigIncrements('id');
		$table->bigInteger('role_id');
		$table->bigInteger('menu_id');
	});
	
	//资讯标签
	APP::schema()->dropIfExists('article_label');
	App::schema()->create('article_label',function($table){
		$table->bigIncrements('id');
		$table->string('name',30);
		$table->string('user_name',30);
		$table->bigInteger('user_id'); //建立者
		$table->timestamp('create_time');
	});
	
	//资讯详细表
	APP::schema()->dropIfExists('article_text');
	App::schema()->create('article_text',function($table){
		$table->bigIncrements('id');
		$table->string('title',100); //标题
		$table->string('summary',2000)->nullable(); //简介
		$table->string('autor',30)->nullable(); //作者
		$table->string('referer',200)->nullable(); //来源
		$table->string('img',200)->nullable(); //新闻图片
		$table->string('out_link',150)->nullable(); //外部链接
		$table->string('user_name',30);
		$table->bigInteger('hit_nums')->default(0); //默认点击数
		$table->bigInteger('rec_nums')->default(0); //默认推荐数
		$table->string('label_json',100)->nullable(); //标签的json
		$table->bigInteger('user_id'); //系统用户
		$table->string('seo_key',300)->nullable(); //seo 关键字
		$table->string('seo_description',600)->nullable(); //seo 描述
		$table->longText('detail')->nullable();
		$table->tinyInteger('status')->default(0);//发布状态：0待发布、1已发布
		$table->bigInteger('orders')->default(1);//排序
		$table->timestamp('create_time');
		$table->timestamp('edit_time');
	});
	APP::DB()->table('article_text')->insert([
		'title' => '本框架特点',
		'summary' => '欢迎使用本框架系统，本为内置帖子，点击查看详细后，可到后台删除...',
		'referer' => '破风手',
		'user_id' => '1',
		'user_name' => 'admin',
		'autor' => 'admin',
		'detail' => '<p>&nbsp;&nbsp;&nbsp;&nbsp;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;这是一套符合中国中小企业以及个人开发的一套轻量级系统，由个人架构完成，现已开源！</p><p>&nbsp;&nbsp;&nbsp;&nbsp;本框架是成品框架，前端自适应各种设备，后台已具备用户、权限管理等功能，只需简单部署便可上线使用！已内置系统管理和博客模块，旨在二次开发时，能快速迭代。<br/></p><p>&nbsp;&nbsp;<span style="font-size: 18px;"><strong>&nbsp;&nbsp;</strong></span></p><p><span style="font-size: 18px;"><strong>&nbsp;&nbsp;&nbsp;&nbsp;本架特点：<br/>&nbsp;&nbsp;&nbsp;&nbsp;</strong></span></p><p>&nbsp;<strong>&nbsp;&nbsp;&nbsp;1.开发快且易，采用JAVA常用企业级技术方案模拟设计</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;2.类库自动引入，只需像JAVA一样声明包路径（PHP称为命名空间）,无需include</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;3.实例引用，采用IOC方式注入，可在方法中声明具体的参数类型，系统自动注入</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;4.事务处理，采用AOP方式，统一声明不具备事务的service方法前缀</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;5.数据层，采用Laravel中的ORM进行建模</strong></p><p><strong><br/></strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;适合做一切类型网站的框架，比如新闻、电影、小说、商城、业务...本框架会一直维持更新，有bug、建议，可在：<a href="http://92sq.com" _src="http://92sq.com">http://92sq.com</a>&nbsp;提出！&nbsp;<br/></strong></p><p><strong><br/></strong></p><p><span style="color: rgb(255, 0, 0);">&nbsp;&nbsp;<strong>&nbsp;&nbsp;本框架运行于PHP5.4 之上，部署请注意！</strong></span></p><p><br/></p><p>&nbsp;&nbsp;&nbsp;&nbsp;<br/></p>',
		'status' => 1,
		'create_time' => Util::time(),
		'edit_time' => Util::time()
    ]);
	//文章与lable的关系
	APP::schema()->dropIfExists('article_label_ship');
	App::schema()->create('article_label_ship',function($table){
		$table->bigIncrements('id');
		$table->bigInteger('label_id');
		$table->bigInteger('article_id');
	});
	//用户推荐记录表
	APP::schema()->dropIfExists('article_user_rec');
	App::schema()->create('article_user_rec',function($table){
		$table->bigIncrements('id');
		$table->bigInteger('user_id');
		$table->bigInteger('article_id');
	});
	
	
	
	echo "①数据库建立完毕.<br/>";
	echo "②后台超级管理账户：admin &nbsp;&nbsp;密码：admin&nbsp;&nbsp;<br/>";
	echo "③你现在需要，访问<a target='_blank' href='".CONTEXT_PATH."/admin/user/login'>后台管理</a>，并初始化菜单[系统管理->菜单查询->生成菜单].<br/>";
	echo "④若二次开发，添加的新菜单需要权限管理，请重新生成菜单.<br/>";
	echo "........已完成........<br/>";